package com.example.admin.brainwave.net;

import android.app.Activity;


import com.example.admin.brainwave.bean.CurrencyBean;
import com.example.admin.brainwave.utils.Const;
import com.example.admin.brainwave.utils.ITaskCallbackListener;
import com.example.admin.brainwave.utils.JsonUtils;
import com.example.admin.brainwave.utils.OkHttpUtil;
import com.example.admin.brainwave.utils.Preconditions;
import com.example.admin.brainwave.utils.ProgressDialogManager;
import com.example.admin.brainwave.utils.RxTask;
import com.example.admin.brainwave.utils.SPHelper;

import java.util.HashMap;

/**
 * Created by Admin on 2018/3/28 0028.
 */

public class UpLoadAvatarTask extends RxTask<String, Integer, CurrencyBean> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public UpLoadAvatarTask(Activity activity, ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }


    @Override
    protected CurrencyBean doInBackground(String... params) {
        String result = OkHttpUtil.postFile(Const.SAVE_IMAGE, getParam(), "imgFile", params[0]);
        CurrencyBean responseInfo = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            responseInfo = JsonUtils.parseObject(result, CurrencyBean.class);
        }
        return responseInfo;
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(CurrencyBean responseInfo) {
        // 执行结果回调函数
        taskListener.doTaskComplete(responseInfo);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(responseInfo);
    }

    private HashMap<String, Object> getParam() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("userId", SPHelper.getInstance(activity).getString("userId", ""));
        return params;
    }

}
